import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 任务一
 * 完成一个命令行工具MathExam，自动生成一份小学一年级的数学加减计算题。
 * 输入：用命令行参数接收一个n，表示出n道题
 * 输出：把题目和标准答案写入out.txt文件
 * @author 王舒宇
 * @create 2018-10-06
 */
public class MathExam2014_Task1 {
    public static void main(String[] args) {

        System.out.println("请输入:想要生成的题目数(1-100)");

        Integer count = Integer.valueOf(args[0]);
        if (args != null && args.length > 0 && isNumeric(args[0])) {

            operation(Integer.valueOf(args[0]), 1);
        } else {
            System.out.println("输入格式错误!"
                    + "请按照如下格式输入:\n想要生成的题目数(1-100)");
        }
        //operation(10,1);
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static void operation(int n, int m) {
        File file = new File("out.txt");
        OutputStream out;
        try {
            out = new FileOutputStream(file);

            int[] an = new int[n];
            int bn[] = new int[n];
            String random = "";
            String[] Symbol1 = {"+", "-"};


            for (int i = 0; i < n; i++) {
                int index = (int) (Math.random() * (Symbol1.length));
                random = Symbol1[index];   //随机调用Symbol1中的元素
                if (random.equals("+")) {
                    int a = (int) (Math.random() * (50 + 1));//随机选择0~50之间的数
                    int b = (int) (Math.random() * (50 + 1));
                    System.out.printf("(" + (i + 1) + ") " + a + " + " + b + " = " + "\n");
                    an[i] = a + b;
                    String output = "(" + (i + 1) + ") " + a + " + " + b + " = " + "\n";
                    out.write(output.getBytes());

                } else {
                    int a = (int) (Math.random() * (100 + 1));
                    int b = (int) (Math.random() * a);
                    System.out.printf("(" + (i + 1) + ") " + a + " - " + b + " = " + "\n");
                    an[i] = a - b;
                    String output = "(" + (i + 1) + ") " + a + " - " + b + " = " + "\n";
                    out.write(output.getBytes());

                }
            }


            //时间
            Date date = new Date();
            String time = "yyyy年MM月 dd日  HH:mm";
            SimpleDateFormat sdf = new SimpleDateFormat(time);
            String now = "\n" + "          201621162014   王舒宇    " + sdf.format(date) + "\n";
            out.write(now.getBytes());


            String flag = "\n";
            out.write(flag.getBytes());
            out.write("标准答案如下：\n\n".getBytes());
            //输入答案
            Scanner in = new Scanner(file);
            int j = 0;
            while (in.hasNextLine() && j != n) {
                if (bn[j] == 0) {
                    String s = in.nextLine() + an[j] + "\n";
                    out.write(s.getBytes());
                } else {
                    String s = in.nextLine() + an[j] + "..." + bn[j] + "\n";
                    out.write(s.getBytes());
                }
                j++;
            }

            out.close();
        } catch (IOException e) {
            System.out.println("输入参数不合法！");
        }
    }

}
